package com.yunhe.action;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;
import com.yunhe.biz.ISubjectBiz;
import com.yunhe.biz.IUserBiz;
import com.yunhe.biz.impl.SubjectBizImpl;
import com.yunhe.biz.impl.UserBizImpl;
import com.yunhe.entity.Subject;
import com.yunhe.entity.User;
/**
 * 登陆
 * */
public class LoginAction extends ActionSupport implements 
ServletRequestAware,ServletResponseAware,SessionAware,ServletContextAware{
	
	private ServletContext application;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map session;
	
	public void setServletRequest(HttpServletRequest request){
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response){
		this.response = response;
	}
	public void setSession(Map session){
		this.session = session;
	}
	public void setServletContext(ServletContext application){
		this.application = application;
	}
	//创建user对象
	private User user;
	//错误信息
	private String error;
	
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	//创建user业务对象
	//IUserBiz ub = new UserBizImpl();
	private IUserBiz ub;
	
	public IUserBiz getUb() {
		return ub;
	}
	public void setUb(IUserBiz ub) {
		this.ub = ub;
	}
	//登陆方法
	public String execute(){
		List<User> list = ub.searchUsers(user);
		if(list.size() != 0){
			for (User user : list) {
				session.put("user", user);
				break;
			}
			
			return "success";
		}else{
			error = "账号或密码错误";
			return "error";
		}
	}
}
